package game

import (
	"time"

	. "gddgame.cc/galaxy/common/dsl/game"
	. "gddgame.cc/galaxy/common/dsl/game/ai_auto"
	. "gddgame.cc/galaxy/common/dsl/game/attr"
	. "gddgame.cc/galaxy/common/dsl/game/badge"
	. "gddgame.cc/galaxy/common/dsl/game/base"
	. "gddgame.cc/galaxy/common/dsl/game/group"
	. "gddgame.cc/galaxy/common/dsl/game/item"
	. "gddgame.cc/galaxy/common/dsl/game/object"
	. "gddgame.cc/galaxy/common/dsl/game/skill"
	. "gddgame.cc/galaxy/common/dsl/game/target"
	. "gddgame.cc/galaxy/common/dsl/game/user"
	"gddgame.cc/galaxy/core/models"
	"gddgame.cc/galaxy/project/star/star/model"

	//. "gddgame.cc/galaxy/common/dsl/game/reward"
	. "gddgame.cc/galaxy/common/dsl/game/stat"
	. "gddgame.cc/galaxy/common/dsl/game/task"
)

type Language struct {
	//Badges   []*Badge
	//BadgeMap map[int]*Badge

	//Items     map[int]*Item
	//ShopItems []*ShopItem

	ZoneNodes []*model.ZoneNodeExt
	ZoneMap   map[string]*model.ZoneNodeExt

	Tasks map[int]*Task
	//
	//ObjectInfoWithObject []*ObjectInfo
	//
	//// 下发的怪物结构
	//ObjectInfoWithTarget map[int]ObjectInfo
}

type Center struct {
	env *Environment
	//// command不会修改，保持引用即可
	//AiGroupConfigs map[int]*AiGroupConfig
	//// command不会修改，保持引用即可
	//Commands map[int]*AiCommand
	//// condition不会修改，保持引用即可
	//Conditions     map[int]*AiConditionTemplate
	//ConditionGroup map[int][]int
	*AiData

	//// skill不会修改，保持引用即可
	//Skills []*Skill
	//// buff不会修改，保持引用即可
	//Buffs []*Buff
	//// attr不会修改，保持引用即可
	//Attrs         []*Attr
	//SettingsAttrs []SettingAttr
	//AttrGroups    []*AttrGroup
	*AttrData
	*SkillData

	//Groups map[int]*SettingGroup
	*GroupData

	//// base不会修改，保持引用即可
	//Bases map[int]*ObjectBaseExt
	//
	//Targets     map[int]*SettingTarget
	//TargetGroup map[int][]int
	//
	//// object不会修改，保持引用即可
	//Objects []*SettingObject
	//
	//// 根据object属性划分
	//ObjectMap   map[int]*SettingObject
	//ObjectGroup map[int][]int

	//TargetGroupStat map[int][]string
	//ObjectGroupStat map[int][]string
	*BaseData
	*ObjectData
	*TargetData

	//// 内部使用
	//// stats不会修改，保持引用即可
	//Stats map[string]*SettingStat
	//// 统计相关
	//GameStats []*SettingStat
	*StatData

	//Badges      map[int]*SettingBadge
	//BadgeParent map[int]*SettingBadge
	*BadgeData

	//Items     map[int]*SettingItem
	//ItemGroup map[int][]int
	//ShopItems map[int]*SettingShopItem
	//ItemGroupStat   map[int][]string
	*ItemData

	// 实际任务
	//GameTasks map[int]*task.GameTask
	*TaskData

	Tasks map[int]*model.SettingTask

	*UserData

	// 分语言统计
	Languages map[string]*Language

	// 预处理任务逻辑
	MaxZoneTaskDifficult int
	MinZoneTaskDifficult int
	ZoneTaskPool         map[model.GameMode]map[TaskType]map[int][]int
}

func NewCenter(env *Environment) *Center {
	return &Center{
		env:       env,
		Languages: make(map[string]*Language, len(env.LanguageList)),
	}
}

func (center *Center) Compute() {
	// 战区任务随机
	center.ZoneTaskPool = make(map[model.GameMode]map[TaskType]map[int][]int)
	center.MinZoneTaskDifficult = len(center.Tasks)
	for id, task := range center.Tasks {
		if task.Type != model.ZoneTask && task.Type != model.ZoneAdditionTask {
			continue
		}
		if len(task.SupportMode) == 0 {
			continue
		}
		if center.MaxZoneTaskDifficult < task.Difficult {
			center.MaxZoneTaskDifficult = task.Difficult
		}
		if center.MinZoneTaskDifficult > task.Difficult {
			center.MinZoneTaskDifficult = task.Difficult
		}
		// 按模式分组
		for _, mode := range task.SupportMode {
			mm, ok := center.ZoneTaskPool[mode]
			if !ok {
				mm = make(map[TaskType]map[int][]int)
				center.ZoneTaskPool[mode] = mm
			}
			modeMap, ok := mm[task.Type]
			if !ok {
				modeMap = make(map[int][]int)
				mm[task.Type] = modeMap
			}
			if _, ok := modeMap[task.Difficult]; !ok {
				modeMap[task.Difficult] = make([]int, 0)
			}
			modeMap[task.Difficult] = append(modeMap[task.Difficult], id)
		}
	}

	// 预处理随机宝箱逻辑
	for key := range model.GiftLogicMap {
		logic := model.GiftLogicMap[key]
		logic.AddItems(center.ItemMap)
		model.GiftLogicMap[key] = logic
	}

	center.initLanguage()
}

func (center *Center) initLanguage() {
	for _, language := range center.env.LanguageList {
		L := &Language{}
		center.Languages[language] = L
	}
}

func (center *Center) InitZoneLanguage(zoneNodeMap map[string]*model.ZoneNode, nextTime *time.Time) {
	for _, language := range center.env.LanguageList {
		L := center.Languages[language]
		items := center.GetItem()

		zoneNodes := make([]*model.ZoneNodeExt, len(zoneNodeMap))
		zoneMap := make(map[string]*model.ZoneNodeExt, len(zoneNodeMap))
		index := 0
		var mainTask *Task
		var additionTask *Task
		mainTask = nil
		additionTask = nil
		for key := range zoneNodeMap {
			node := zoneNodeMap[key]
			if node.MainTaskInfo != nil {
				mainTask = NewTask(node.MainTaskInfo, language, items)
			}
			if node.AdditionTaskInfo != nil {
				additionTask = NewTask(node.AdditionTaskInfo, language, items)
			}

			zoneNodes[index] = model.NewZoneNode(node, mainTask, additionTask, nextTime, center.Targets, language)
			zoneMap[node.Key] = zoneNodes[index]
			index += 1
		}

		L.ZoneNodes = zoneNodes
		L.ZoneMap = zoneMap
	}
}

func (center *Center) MakeTarget(id int, language string) ObjectInfo {
	return center.GetTarget(language)[id]
}

func (center *Center) GetUserFromOpen(open *models.UserOpen) UserBase {
	var u model.User
	_ = center.FindUser(open.Id, &u)

	// 绑定道具信息
	var items []model.UserItem
	if err := center.GetUserItems(&u, append(u.Items, u.Weapon, u.Machine), &items); err != nil {
		return &u
	}
	u.ItemMap = make(map[int]*model.UserItem, len(items))
	for index := range items {
		item := items[index]
		item.Change = false
		u.ItemMap[item.Id] = &item
	}
	return &u
}
